package com.ibeetl.admin.console.web;

import com.ibeetl.admin.console.service.BasComConsoleService;
import com.ibeetl.admin.console.web.query.BasComQuery;
import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.entity.BasCom;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.util.ConvertUtil;
import com.ibeetl.admin.core.util.ValidateConfig;
import com.ibeetl.admin.core.web.JsonResult;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 用户管理接口
 * 
 * @author luhao
 */
@Controller
public class ComConsoleController {
	private final Log log = LogFactory.getLog(this.getClass());
	private static final String MODEL = "/admin/com";

	@Autowired
	BasComConsoleService basComConsoleService;
	@Autowired
	CorePlatformService platformService;

	/**
	 * 显示机构列表
	 *
	 * @return
	 */
	@GetMapping(MODEL + "/index.do")
	@Function("com")
	public ModelAndView index() {
		ModelAndView view = new ModelAndView("/admin/com/index.html");
		view.addObject("search", BasComQuery.class.getName());
		return view;
	}

	/**
	 * 查询机构
	 *@param condtion
	 *
	 * @return
	 */

	@PostMapping(MODEL + "/list.json")
	@Function("com.query")
	@ResponseBody
	public JsonResult<PageQuery<BasCom>> index(BasComQuery condtion) {
		PageQuery<BasCom> page = condtion.getPageQuery();
		basComConsoleService.queryByCondtion(page);
		return JsonResult.success(page);
	}

	/**
	 * 显示新增页面
	 * @return
	 */
	@GetMapping(MODEL + "/add.do")
	@Function("com.add")
	public ModelAndView add() {
		ModelAndView view = new ModelAndView("/admin/com/add.html");
		return view;
	}

	/**
	 * 添加机构
	 * @param basCom 新增的机构对象
	 * @return
	 */
	@PostMapping(MODEL + "/add.json")
	@Function("com.add")
	@ResponseBody
	public JsonResult<Long> add(@Validated(ValidateConfig.ADD.class) BasCom basCom) {

		basComConsoleService.saveCom(basCom);
		return JsonResult.success(basCom.getId());
	}


	/**
	 * 编辑机构
	 *@param id 机构id
	 *
	 * @return
	 */
	@GetMapping(MODEL + "/edit.do")
	@Function("com.edit")
	public ModelAndView edit(String id) {
		ModelAndView view = new ModelAndView("/admin/com/edit.html");
		BasCom basCom = basComConsoleService.queryById(id);
		view.addObject("basCom", basCom);
		return view;
	}

	/**
	 * 更新用户信息
	 *@param basCom 用户对象
	 *
	 * @return
	 */
	@PostMapping(MODEL + "/update.json")
	@Function("com.update")
	@ResponseBody
	public JsonResult update(@Validated(ValidateConfig.UPDATE.class) BasCom basCom) {
		boolean success = basComConsoleService.update(basCom);
		if (success) {
			this.platformService.clearFunctionCache();
			return JsonResult.success();
		} else {
			return JsonResult.failMessage("保存失败！");
		}
	}

    /**
     * 删除机构
     * @param ids 组织id，多个用“,”隔开
     * @return
     */
    @PostMapping(MODEL + "/delete.json")
    @Function("com.delete")
    @ResponseBody
    public JsonResult delete(String ids) {
        List<Long> dels = ConvertUtil.str2longs(ids);
        basComConsoleService.batchDelCom(dels);
//		List<Long> dels = ConvertUtil.str2longs(ids);
//		basUserConsoleService.batchDelUser(dels);
        return JsonResult.success();
    }
}
